package com.wenan.ui.controls.charts
{
	import com.amcharts.AmLegend;
	import com.amcharts.chartClasses.ChartScrollbar;
	import com.wenan.ui.controls.charts.expand.AmChartSpecial;
	import com.wenan.ui.controls.charts.expand.AmGraphExpand;
	import com.wenan.ui.controls.charts.expand.AmLegendEvent;
	import com.wenan.ui.controls.charts.expand.AmLegendItemRenderer;
	import com.wenan.ui.controls.charts.expand.AmLegendSpecial;
	import com.wenan.ui.controls.charts.expand.CategoryAxisExpand;
	import com.wenan.ui.controls.listBoxes.CheckListBoxItemEvent;
	import com.wenan.ui.controls.listBoxes.TitleCheckListBox;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import mx.containers.Canvas;
	import mx.containers.HBox;
	import mx.containers.VBox;
	import mx.controls.Button;
	import mx.controls.Label;
	import mx.controls.Spacer;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	import mx.rpc.events.ResultEvent;
	
	/**
	 * 簇状
	 * */
	public class Clumps extends SuperAmChartsModule
	{
		public var fileName:String;
		public var listData:Object;
		public var listLabelField:String;
		public var categoryField:String;
		public var valueDescribeList:ArrayCollection = new ArrayCollection();
		
		public var cityId:String;
		private var amLegend:AmLegendSpecial = null;
		public function Clumps()
		{
			super();
		}
		
		public override function chartHandler(obj:Object=null):void
		{
			var v2:Canvas = new Canvas();
			v2.setStyle("horizontalAlign", "right");
			v2.percentHeight = 100;
			v2.percentWidth = 100;
			var type:Number = 3;
			acs = new AmChartSpecial(type, dataProvider);
			acs.addEventListener(FlexEvent.CREATION_COMPLETE, function (event:FlexEvent):void {
				initAxis();
			});
			var amGraph:AmGraphExpand;
			acs.id = "amChart";
			acs.percentWidth = 100;
			acs.percentHeight = 98;
			acs.categoryField = categoryField;
			acs.rotate = false;
			acs.colors = colors.source;
			acs.startDuration(0.5);
			acs.amChart.setStyle("depth3D", depth3D);
			acs.amChart.setStyle("angle", angle);
			
			var left:Button = new Button();
			var right:Button = new Button();
			left.autoRepeat = true;
			right.autoRepeat = true;
			left.styleName = "LeftButton";
			right.styleName = "RightButton";
			left.addEventListener(FlexEvent.BUTTON_DOWN, left_clickHandler);
			right.addEventListener(FlexEvent.BUTTON_DOWN, right_clickHandler);
			
			for each (var vd:Object in valueDescribeList)
			{
				amGraph = new AmGraphExpand();
				amGraph.title = vd.title;
				amGraph.valueField = vd.propertyName;
				amGraph.color = vd.color;
				amGraph.chartType = type;
				amGraph.showBalloon=true;
				amGraph.lineAlpha = 0.8;
				amGraph.fillAlphas = [0.8];
				amGraph.setStyle("cornerRadiusTop", 8);
				amGraph.balloonText = balloonText;
				acs.graphsList.push(amGraph);
			}
			
			acs.categoryAxis(getCategoryAxis());
			acs.valueAxes(getValueAxis());
			acs.balloon(getAmBalloon());
			acs.chartCursor(getChartCursor());
			
			amLegend = new AmLegendSpecial();
			amLegend.percentWidth = 100;
			amLegend.dataProvider = acs.amChart;
			amLegend.addEventListener(AmLegendEvent.AMLEGENDASC, amlegendAscHandler);
			amLegend.addEventListener(AmLegendEvent.AMLEGENDDESC, amlegendDescHandler);
			amLegend.addEventListener("showScroll", function(event:Event):void {
				left.visible = true;
				right.visible = true;
			});
//			cs.graph = amGraph;
			acs.chartScrollbar(cs);
			v2.addElement(acs);
			v2.addElement(axisXImage);
			v2.addElement(axisYImage);
			
			var hbox:HBox = new HBox();
			left.visible = false;
			right.visible = false;
			hbox.width = 755;
			hbox.setStyle("verticalAlign", "middle");
			hbox.addChild(dateLabel);
			hbox.addChild(left);
			hbox.addChild(amLegend);
			hbox.addChild(right);
			hbox.setStyle("bottom", 0);
			hbox.setStyle("right", 8);
			
			v2.addElement(hbox);
			reportBox.addChild(v2);
		}
		
		private function amlegendAscHandler(event:AmLegendEvent):void
		{
			var fieldName:String = event.filedName;
			var temp:ArrayCollection = dataProvider as ArrayCollection;
			var sort:Sort = new Sort();
			sort.fields = [new SortField(fieldName, false)];
			temp.sort = sort;
			temp.refresh();
		}
		
		private function amlegendDescHandler(event:AmLegendEvent):void
		{
			var fieldName:String = event.filedName;
			var temp:ArrayCollection = dataProvider as ArrayCollection;
			var sort:Sort = new Sort();
			sort.fields = [new SortField(fieldName, false, true)];
			temp.sort = sort;
			temp.refresh();
		}
		
		public override function createReportPanel():UIComponent 
		{
			reportBox = new VBox();
			reportBox.setStyle("backgroundColor", 0xF1F8FE);
			reportBox.setStyle("borderColor", 0xBDCCDB);
			reportBox.setStyle("borderStyle", "solid");
			reportBox.setStyle("horizontalAlign", "center");
			reportBox.percentWidth = 100;
			reportBox.percentHeight = 100;
			
			var name:Label = new Label();
			name.text = titleName;
			name.setStyle("fontSize", titleSize);
			name.setStyle("color", titleColor);
			name.setStyle("fontWeight", "bold");
			name.setStyle("horizontalCenter", 0);
			var yHead:Label = new Label();
			yHead.text = yHeading.title;
			yHead.setStyle("color", yHeading.titleTextColor);
			yHead.setStyle("fontSize", yHeading.titleTextSize);
			yHead.setStyle("left", 20);
			yHead.setStyle("top", 20);
			var ex:Canvas = new Canvas();
			ex.percentWidth = 100;
			ex.addChild(name);
			ex.addChild(yHead);
			reportBox.addChild(ex);
			
			chartHandler();
			return reportBox;
		}
		
		protected function itemSelectedHandler(event:CheckListBoxItemEvent):void 
		{
			/*
			for each (var vd:Object in valueDescribeList)
			{
				if (temp == vd.propertyName)
				{
					flag = 1;
					colors.removeItemAt(i);
					valueDescribeList.removeItemAt(i);
				}
				i++;
			}
			if (flag == 0)
			{
				var o:Object = new Object();
				o.title = target.title;
				o.propertyName = target.propertyName;
				o.color = target.color;
				colors.addItem(target.color);
				valueDescribeList.addItem(o);
			}
			*/
			var s:Object = event.item;
		}
		
		protected function initAxis():void
		{
			axisXImage.x=acs.x+acs.width-40;
			axisXImage.y=acs.y+acs.height-acs.amChart.getStyle("marginBottom") -4;
			
			axisYImage.x=acs.amChart.getStyle("marginLeft") - 5;
			axisYImage.y=acs.y+30;
		}
		
		protected function left_clickHandler(event:FlexEvent):void
		{
			amLegend.dispatchEvent(new Event("leftScroll", true, true));
		}
		protected function right_clickHandler(event:FlexEvent):void
		{
			amLegend.dispatchEvent(new Event("rightScroll", true, true));
		}
		
	}
}